package com.ruoyi.parkinglot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.parkinglot.mapper.ParkLotMapper;
import com.ruoyi.parkinglot.service.ParkLotService;
import com.ruoyi.parkinglot.domain.ParkLot;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
* @author 31713
* @description 针对表【park_lot(停车场表 - 用于管理停车场基础信息及实时状态)】的数据库操作Service实现
* @createDate 2025-02-11 13:43:45
*/
@Service
public class ParkLotServiceImpl extends ServiceImpl<ParkLotMapper, ParkLot>
    implements ParkLotService {

    /**
     * 查询停车场列表
     * @param lotName 停车场名称
     * @param lotType 停车场类型
     * @return 停车场列表
     */
    @Override
    public List<ParkLot> selectParkLotList(String lotName, String lotType) {
        LambdaQueryWrapper<ParkLot> queryWrapper = new LambdaQueryWrapper<>();

        // 添加名称查询条件
        if (lotName != null && !lotName.isEmpty()) {
            queryWrapper.like(ParkLot::getLotName, lotName);
        }

        // 添加类型查询条件
        if (lotType != null && !lotType.isEmpty()) {
            queryWrapper.eq(ParkLot::getLotType, lotType);
        }

        queryWrapper.orderByDesc(ParkLot::getCreateTime);
        return this.list(queryWrapper);
    }
    /**
     * 新增停车场
     */
    @Override
    public int insertParkLot(ParkLot parkLot) {
        return this.baseMapper.insert(parkLot);
    }
    /**
     * 修改停车场
     */
    @Override
    public int updateParkLot(ParkLot parkLot) {
        return this.baseMapper.updateById(parkLot);
    }
    /**
     * 删除停车场
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteParkLotByIds(Long[] lotIds) {
        if (lotIds == null || lotIds.length == 0) {
            return 0;
        }

        // 检查这些停车场是否存在
        List<ParkLot> existingLots = this.listByIds(Arrays.asList(lotIds));
        if (existingLots.isEmpty()) {
            return 0;
        }

        // 执行批量删除
        return this.baseMapper.deleteBatchIds(Arrays.asList(lotIds));
    }
    /**
     * 获取停车场详细信息
     */
    @Override
    public ParkLot selectParkLotById(Long lotId) {
        return this.getById(lotId);
    }
}




